package org.btik.random_block;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import static org.btik.random_block.MaterialUtil.getRandomMaterial;

public final class RandomBlock extends JavaPlugin {

    @Override
    public void onEnable() {
        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {

            Server server = getServer();

            for (World world : server.getWorlds()) {
                if (world.getPlayers().isEmpty()) {
                    continue;
                }
                Location spawnLocation = world.getSpawnLocation();
                for (int i = -3; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        for (int k = -3; k < 3; k++) {
                            Material randomMaterial = getRandomMaterial();
                            Location location = spawnLocation.clone().add(i, j, k);
                            if (randomMaterial.isBlock()) {
                                world.getBlockAt(location).setType(randomMaterial);
                            } else if (randomMaterial.isItem()) {
                                world.dropItem(location, new ItemStack(randomMaterial));
                            }
                        }
                    }
                }
            }
            server.sendMessage(Component.text("Random block spawned", NamedTextColor.GREEN));
        }, 0L, 20 * 3 * 60);
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}
